/**
 * Copyright (C) 2011 Droidhang Inc.
 * All Rights Reserved.
 *
 * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Droidhang Inc.
 * The contents of this file may not be disclosed to third parties, copied or
 * duplicated in any form, in whole or in part, without the prior written
 * permission of Droidhang Inc.
 *
 * @author michael.wang
 *
 */

#include "log.h"
#include "cocos2d.h"

using namespace std;

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include <android/log.h>
#include <stdarg.h>
#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS
#import <Foundation/Foundation.h>
#endif

const string getLogTag(const string& str, int ln) {
    string s(str);
    string::size_type pos = s.find_last_of('/');
    if (pos != string::npos) {
        s = s.substr(pos+1);
    }
    char temp[10];
    sprintf(temp, ":%d", ln);
    return s+temp;
}

#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID

static void log(const char *tag, int prio, const char *fmt, va_list argp) {
    __android_log_vprint(prio, tag, fmt, argp);
}

void dhlog(const string& tag, const char *fmt, ...) {
    va_list argp;
    va_start(argp, fmt);
    string strFmt = string(fmt)+"\n";
    log(tag.c_str(), ANDROID_LOG_ERROR, strFmt.c_str(), argp);
    va_end(argp);
}

#elif CC_TARGET_PLATFORM == CC_PLATFORM_IOS

#define IOSLOGE(fmt, args) NSLogv(fmt, args)

void dhlog(const string& tag, const char *fmt, ...) {
    string str = tag+" "+fmt+"\n";
    va_list argp;
    va_start(argp, fmt);
    IOSLOGE([NSString stringWithUTF8String:str.data()], argp);
    va_end(argp);
}
#else	// WIN32
#include <stdarg.h>
#include <Windows.h>

#define MAX_LEN 100000
void dhlog(const string& tag, const char *fmt, ...) {
	string str = tag+" "+fmt+"\n";
	char szBuf[MAX_LEN];
	va_list ap;
	va_start(ap, fmt);
	vsnprintf_s(szBuf, MAX_LEN, MAX_LEN, str.c_str(), ap);
	va_end(ap);
    
	WCHAR wszBuf[MAX_LEN] = {0};
	MultiByteToWideChar(CP_UTF8, 0, szBuf, -1, wszBuf, sizeof(wszBuf));
	OutputDebugStringW(wszBuf);
}


#endif
